/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.hanhishi;

import static br.com.sales.hanserver.action.hanhishi.consts.HanHisHIConsts.GET_DATA;
import static br.com.sales.hanserver.action.hanhishi.consts.HanHisHIConsts.GET_DATA_ITEM;
import br.com.sales.hanserver.dao.Dao;
import static br.com.sales.hanserver.dao.Dao.closeConn;
import static br.com.sales.hanserver.dao.Dao.getConn;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v
 */
public class GetDataHistoric extends Dao {

    public GetDataHistoric() {
    }

    public List<Map<String, Object>> getData(Map paramters) {
        List<Map<String, Object>> resultData = new LinkedList<>();
        try {

            StringBuilder sql = new StringBuilder(GET_DATA);

            if (paramters != null) {

                if (paramters.get("IE_FILTRAR") != null && "S".equals((String) paramters.get("IE_FILTRAR"))
                        && paramters.get("START") != null && paramters.get("FINISH") != null) {
                    sql.append(" and dt_venda between STR_TO_DATE('").append(paramters.get("START")).append("','%d/%m/%Y %T')");
                    sql.append(" and STR_TO_DATE('").append(paramters.get("FINISH")).append("','%d/%m/%Y %T')");
                }

                if (paramters.get("NM_CLIENTE") != null && !"".equals(paramters.get("NM_CLIENTE"))) {
                    sql.append(" and upper(nm_pessoa_fisica) like '%").append(((String) paramters.get("NM_CLIENTE")).toUpperCase()).append("%' ");
                }

            }

            PreparedStatement st = getConn().prepareStatement(sql.toString());

            ResultSet rs = st.executeQuery();
            ResultSet rsItem;

            PreparedStatement stItem = getConn().prepareStatement(GET_DATA_ITEM);
            Long sequence;
            while (rs.next()) {
                Map mainMap = new LinkedHashMap(3);
                Map map = new LinkedHashMap(3);

                sequence = rs.getLong("NR_SEQUENCIA");
                map.put("DT_VENDA", rs.getString("DT_VENDA"));
                map.put("VL_TOTAL_VENDA", rs.getString("VL_TOTAL_VENDA"));
                map.put("CLIENT", rs.getString("CLIENT"));
                mainMap.put("SALES", map);

                stItem.setLong(1, sequence);
                rsItem = stItem.executeQuery();
                Set items = new LinkedHashSet(3);
                while (rsItem.next()) {
                    Map mapItems = new LinkedHashMap(3);
                    mapItems.put("DS", rsItem.getString("DS"));
                    mapItems.put("QT", rsItem.getInt("QT"));
                    mapItems.put("VL", rsItem.getString("VL"));
                    items.add(mapItems);
                }
                mainMap.put("ITEMS", items);
                resultData.add(mainMap);

            }

            return resultData;

        } catch (Exception ex) {
            Logger.getLogger(GetDataHistoric.class.getName()).log(Level.SEVERE, "Error getData", ex);
        } finally {
            closeConn();
        }
        return resultData;
    }
}
